


//
//  BaseCheckHomeworkListViewController.m
//  TeacherPro
//
//  Created by DCQ on 2017/8/12.
//  Copyright © 2017年 ZNXZ. All rights reserved.
//

#import "BaseCheckHomeworkListViewController.h"
#import "CheckHomeworkListModel.h"
#import "CheckHomeworkCell.h"

#import "HomeworkDetailViewController.h"
#import "RewardStudentViewController.h"
#import "CheckHomeworkDetailVC.h"
#import "UIViewController+HBD.h"

#import "HWReportViewController.h"
#import "InvitationStudentViewController.h"


NSString * const BaseCheckHomeworkCellIdentifier = @"BaseCheckHomeworkCellIdentifier";
@interface BaseCheckHomeworkListViewController ()


@end

@implementation BaseCheckHomeworkListViewController

- (void)viewDidLoad {
    
    self.currentPageNo = 0;
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( reloadListViewData) name:@"UPDATA_HOMEWORK_LIST_DATA" object:nil];
}

- (void)reloadListViewData{
    if (self.checkSuccessBlock) {
        self.checkSuccessBlock();
    }
    [self beginRefresh];
    
}
- (void)registerCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CheckHomeworkCell class]) bundle:nil] forCellReuseIdentifier:BaseCheckHomeworkCellIdentifier];
    
}

- (void)getNormalTableViewNetworkData{
    
    [self requestHomework];
     
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger )getCheckHomeworkType{
    
    return 0;
}

- (void)requestCallHomework:(NSString *)homeworkId{
    NSDictionary *parameterDic = @{@"homeworkId":homeworkId};
    [self sendHeaderRequest:[NetRequestAPIManager getRequestURLStr:NetRequestType_TeacherCallHomework] parameterDic:parameterDic requestMethodType:RequestMethodType_POST requestTag:NetRequestType_TeacherCallHomework];
}

- (void)requestHomework{
    
    NSString *pageIndex = [NSString stringWithFormat:@"%zd",self.currentPageNo];
    NSString *pageSize = [NSString stringWithFormat:@"%zd",self.pageCount];
    NSString * remarked = @"false";
    if ([self getCheckHomeworkType] == 0) {
        remarked = @"false";
    }else if ([self getCheckHomeworkType] == 1){
        
        remarked = @"true";
    }
    
    NSDictionary * parameterDicTemp = @{@"pageIndex":pageIndex,
                                        @"pageSize":pageSize,
                                        @"remarked":remarked,
                                        };
    NSMutableDictionary * parameterDic = [NSMutableDictionary dictionaryWithDictionary:parameterDicTemp];
    if (self.clazzId ) {
        [parameterDic addEntriesFromDictionary:@{@"clazzId":self.clazzId}];
    }
    
    [self sendHeaderRequest:[NetRequestAPIManager getRequestURLStr:NetRequestType_ListTeacherHomeworks] parameterDic:parameterDic requestMethodType:RequestMethodType_POST requestTag:NetRequestType_ListTeacherHomeworks];
}

- (void)setNetworkRequestStatusBlocks{
    
    WEAKSELF
    [self setNetSuccessBlock:^(NetRequest *request, id successInfoObj) {
        STRONGSELF
        if (request.tag == NetRequestType_ListTeacherHomeworks) {
            strongSelf.hasLoadData = YES;
            if (strongSelf.currentPageNo == 0) {
                strongSelf.checkModels = nil;
            }
            if (!strongSelf.checkModels) {
                strongSelf.checkModels = [[CheckHomeworkListModel alloc]initWithDictionary:successInfoObj error:nil];
                strongSelf.currentPageNo ++;
            }else {
                
                CheckHomeworkListModel * tempModel = [[CheckHomeworkListModel alloc]initWithDictionary:successInfoObj error:nil];
                if (tempModel.homeworks.count >0) {
                    [strongSelf.checkModels.homeworks addObjectsFromArray:tempModel.homeworks];
                    strongSelf.currentPageNo ++;
                }
                
            }
        } else if (request.tag == NetRequestType_TeacherCallHomework){
            if ( successInfoObj[@"studentCount"]) {
                NSString * content = [NSString stringWithFormat:@"成功向%@名学生发送催缴消息",successInfoObj[@"studentCount"]];
                [strongSelf showAlert:TNOperationState_OK content:content];
            }
            
            
        }
        
        [strongSelf updateTableView];
    }];
}


- (BOOL)isAddRefreshHeader{
    BOOL yesOrNo = YES;
    
    return yesOrNo;
}

- (BOOL)isAddRefreshFooter{
    BOOL yesOrNo = YES;
    
    return yesOrNo;
}

- (CGRect)getTableViewFrame{
    CGRect tableViewFrame = CGRectZero;
    CGFloat SegmentedViewHeight  =  FITSCALE(44);
    CGFloat spacing = 8;
    tableViewFrame = CGRectMake(0, spacing,self.view.frame.size.width, self.view.frame.size.height - SegmentedViewHeight- spacing);
    return tableViewFrame;
}
- (void)drogDownRefresh{
    self.currentPageNo = 0;
    self.pageCount = 30;
    [self getNormalTableViewNetworkData];
}
- (void)getLoadMoreTableViewNetworkData{
    
    [self getNormalTableViewNetworkData];
    
}

- (NSInteger)getNetworkTableViewDataCount{
    NSInteger  count = 0;
    
    count = self.checkModels.homeworks.count;
    
    return  count;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger count = 0;
    count = [self.checkModels.homeworks  count];
    return count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (iPhone5) {
        return 234 -20;
    }else
        return FITSCALE(214)-20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return FITSCALE(7);
}
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIImageView * footV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, FITSCALE (7))];
    [footV setImage:[UIImage imageNamed:@"speack_line"]];
    return footV;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =nil;
    CheckHomeworkCell * tempCell = [ tableView dequeueReusableCellWithIdentifier:BaseCheckHomeworkCellIdentifier];
    BOOL isRemarked = NO;
    CheckHomeworkModel * model = nil;
    
    if ([self getCheckHomeworkType] == 0) {
        isRemarked = NO;
        
    }else if ([self getCheckHomeworkType] == 1){
        isRemarked = YES;
        
    }
    model = self.checkModels.homeworks[indexPath.section];
    [tempCell setupHomeworkInfo:model isRemarked:isRemarked];
    tempCell.checkeBlock = ^(CheckHomeworkModel *model, NSInteger type) {
        switch (type) {
            case CheckHomeworkCellButtonType_worth:
                [self worthHomework:model.homeworkId];
                break;
            case CheckHomeworkCellButtonType_look:
                //                [self gotoLookHomeworkVC:model ];
                [self gotoCheckOrLookHomeworkVC:type withHomeworkId:model.homeworkId withOnlineHomework:model.onlineHomework];
                break;
            case CheckHomeworkCellButtonType_check:
                //                [self gotoCheckeHomeworkVC:model ];
                [self gotoCheckOrLookHomeworkVC:type withHomeworkId:model.homeworkId withOnlineHomework:model.onlineHomework];
                break;
            case CheckHomeworkCellButtonType_detail:
                [self gotoHomeworkDetailVC:model];
                break;
            case CheckHomeworkCellButtonType_reprot:
                [self gotoReportVC:model];
                break;
            case CheckHomeworkCellButtonType_InviteStudents:
                [self gotoInviteStudents:model];
                break;
            default:
                break;
        }
    };
    tempCell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell = tempCell;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return  FITSCALE(0);
    }else
        return  FITSCALE(0);
    
}

- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;{
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH,  FITSCALE(20))];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

#pragma mark ---
//催缴作业
- (void)worthHomework:(NSString *)homeworkId{
    
    [self requestCallHomework:homeworkId];
    
}
//作业详情
- (void)gotoHomeworkDetailVC:(CheckHomeworkModel *)model{
    
     HomeworkDetailViewController * detailVC = [[HomeworkDetailViewController alloc]initWithHomeworkId:model.homeworkId];
     [self pushViewController:detailVC];
     
}
//作业报告
- (void)gotoReportVC:(CheckHomeworkModel *)model{
    HWReportViewController * detailVC = [[HWReportViewController alloc]initWithHomeworkId:model.homeworkId];
    [self pushViewController:detailVC];
}

- (void)gotoInviteStudents:(CheckHomeworkModel *)model{
    InvitationStudentViewController * studentVC = [[InvitationStudentViewController alloc]initWithClazzName:model.clazzName withClazzId:model.clazzId];
    [self pushViewController:studentVC];
    
}
//查看作业
//- (void)gotoLookHomeworkVC:(CheckHomeworkModel *)model{
//    HomeworkBackfeedType  type;
//    if ([model.feedbackName isEqualToString:@"不需要反馈"]) {
//        type = HomeworkBackfeedType_nono;
//    }else{
//        type = HomeworkBackfeedType_backfeed;
//    }
//
//    RewardStudentViewControlleType homeworkType = RewardStudentViewControlleType_normal ;
//    if ([model.onlineHomework boolValue]) {
//        homeworkType = RewardStudentViewControlleType_lookOnlineHomework;
//    }else{
//
//        homeworkType = RewardStudentViewControlleType_lookUnonlineHomework;
//    }
//    RewardStudentViewController * rewardVC = [[RewardStudentViewController alloc]initWithRewardType:homeworkType withTitle:model.clazzName withHomeworkId:model.homeworkId  withHomeworkBackfeedType:type];
//
//
//    [self.navigationController pushViewController:rewardVC animated:YES];
//}
//检查作业
//- (void)gotoCheckeHomeworkVC:(CheckHomeworkModel *)model{
//    HomeworkBackfeedType  type;
//    if ([model.feedbackName isEqualToString:@"不需要反馈"]) {
//        type = HomeworkBackfeedType_nono;
//    }else{
//        type = HomeworkBackfeedType_backfeed;
//    }
//
//    RewardStudentViewControlleType homeworkType = RewardStudentViewControlleType_normal ;
//    if ([model.onlineHomework boolValue]) {
//        homeworkType = RewardStudentViewControlleType_checkOnlineHomework;
//    }else{
//
//        homeworkType = RewardStudentViewControlleType_checkUnonlineHomework;
//    }
//    RewardStudentViewController * rewardVC =  [[RewardStudentViewController alloc]initWithRewardType:homeworkType  withTitle:model.clazzName withHomeworkId:model.homeworkId  withHomeworkBackfeedType:type];
//    WEAKSELF
//    rewardVC.checkSuccessBlock = ^{
//        if (weakSelf.checkSuccessBlock) {
//            weakSelf.checkSuccessBlock();
//        }
//         [weakSelf beginRefresh];
//    };
//    [self.navigationController pushViewController:rewardVC animated:YES];
//
//}


- (void)gotoCheckOrLookHomeworkVC:(CheckHomeworkCellButtonType) type withHomeworkId:(NSString *)homeworkId withOnlineHomework:(NSNumber *)onlineHomework{
    CheckHomeworkDetailVCType  detailType = CheckHomeworkDetailVCType_normal;
    if (type ==   CheckHomeworkCellButtonType_look){
        detailType = CheckHomeworkDetailVCType_look;
    }else if (type ==   CheckHomeworkCellButtonType_check) {
        detailType = CheckHomeworkDetailVCType_check;
    }
    CheckHomeworkDetailVC  * detailVC = [[CheckHomeworkDetailVC alloc]initHomeworkID:homeworkId withType:detailType withOnlineHomework:onlineHomework];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

@end

